package org.ting.e_commerce.controller;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ting.e_commerce.enums.ResultStatus;
import org.ting.e_commerce.model.Product;
import org.ting.e_commerce.model.Result;
import org.ting.e_commerce.service.ProductService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    @RequestMapping("/addProduct")
    public Result<?> addProduct(Product product) {
        if(productService.addProduct(product)) {
            return Result.success(ResultStatus.SUCCESS);
        }
        return Result.fail("添加失败");
    }

    @RequestMapping("/selectByStoreAndStatus")
    public List<Product> selectByStoreAndStatus(Integer storeId, String productStatus) {
        return productService.selectByStoreAndStatus(storeId, productStatus);
    }
    @RequestMapping("/selectAll")
    public List<Product> selectAll(int page, int pageSize) {
        return productService.selectAll(page, pageSize);
    }

    @RequestMapping("/updateById")
    public boolean updateById(Product product) {
        return productService.updateById(product);
    }

    @RequestMapping("/selectByStatus")
    public List<Product> selectByStatus(String productStatus, int page, int pageSize) {
        return productService.selectByStatus(productStatus, page, pageSize);
    }
    @RequestMapping("/selectPageCountOfStatus")
    public int selectCountOfStatus(String productStatus, int pageSize) {
        return productService.selectPageCountOfStatus(productStatus, pageSize);
    }
    @RequestMapping("/deleteById")
    public boolean deleteById(Integer productId) {
        return productService.deleteById(productId);
    }
    @RequestMapping("/selectByStatusAndWords")
    public List<Product> selectByStatusAndWords(String productStatus, String words, int page, int pageSize) {
        System.out.println("++++++++++++++++++");
        System.out.println(words);
        System.out.println("++++++++++++++++++++");
        if(words == null || words.equals(" ")) {
            return productService.selectByStatus(productStatus, page, pageSize);
        }
        return productService.selectByStatusAndWords(productStatus, words, page, pageSize);
    }
    @RequestMapping("/selectPageCountOfStatusAndWords")
    public int selectPageCountOfStatusAndWords(String productStatus, String words, int pageSize) {
        System.out.println("++++++++++++++++++");
        System.out.println(words);
        System.out.println("++++++++++++++++++++");
        if(words == null || words.equals(" ")) {
            return productService.selectPageCountOfStatus(productStatus, pageSize);
        }
        return productService.selectPageCountOfStatusAndWords(productStatus, words, pageSize);

    }
}
